﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Koala.Pro.Common;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using Koala.Pro.Models;
using Koala.Pro.Services;

namespace Koala.Pro.ViewModels
{
    public partial class FunctionBatchAddViewModel : BaseViewModel
    {
        [ObservableProperty]
        private BatchAddFunctionDto? item = new BatchAddFunctionDto();

        [ObservableProperty]
        private List<CListItem> subFunctions = new List<CListItem>
    {
        new CListItem
        {
            Value = "Add",
            Text = "添加"
        },
        new CListItem
        {
            Value = "Delete",
            Text = "删除"
        },
        new CListItem
        {
            Value = "Edit",
            Text = "修改"
        },
        new CListItem
        {
            Value = "View",
            Text = "查看"
        },
        new CListItem
        {
            Value = "Import",
            Text = "导入"
        },
        new CListItem
        {
            Value = "Export",
            Text = "导出"
        }
    };

        [ObservableProperty]
        private bool isReadOnly;



        public FunctionBatchAddViewModel()
        {
            base.Title = "批量添加字典项目";
            Item.SubFunctions = SubFunctions.Select((CListItem s) => s.Text).ToList();
            OnPropertyChanged("Item");
        }

        [RelayCommand]
        public async Task Save()
        {
            bool success = false;
            string text;
            try
            {
                success = await InsertOrUpdate();
                text = (success ? "操作成功" : "操作失败");
            }
            catch (Exception ex)
            {
                LogTextHelper.Error(ex);
                text = ex.Message;
            }
            if (success)
            {
                Growl.SuccessGlobal(text);
            }
            else
            {
                Growl.ErrorGlobal(text);
            }
            base.Result = new CommonResult(success, text);
        }

        protected virtual async Task<bool> InsertOrUpdate()
        {
            if (Item == null)
            {
                return false;
            }
            FunctionInfo mainInfo = new FunctionInfo
            {
                Name = Item.Name,
                PID = Item.PID,
                ControlID = Item.ControlID,
                SystemType_ID = Item.SystemType_ID,
                SortCode = Item.SortCode
            };
            bool sucess = await BLLFactory<IFunctionService>.Instance.InsertAsync(mainInfo);
            if (sucess)
            {
                int sortCodeIndex = 1;
                foreach (string sub in Item.SubFunctions)
                {
                    CListItem cListItem = SubFunctions.Single((CListItem s) => s.Text == sub);
                    if (cListItem != null)
                    {
                        FunctionInfo functionInfo = CreateSubFunction(mainInfo);
                        functionInfo.SortCode = sortCodeIndex++.ToString("D2");
                        functionInfo.ControlID = $"{mainInfo.ControlID}:{cListItem.Value}";
                        functionInfo.Name = $"{cListItem.Text}{mainInfo.Name}";
                        await BLLFactory<IFunctionService>.Instance.InsertAsync(functionInfo);
                    }
                }
            }
            return sucess;
        }

        private FunctionInfo CreateSubFunction(FunctionInfo mainInfo)
        {
            return new FunctionInfo
            {
                PID = mainInfo.Id,
                SystemType_ID = mainInfo.SystemType_ID
            };
        }
    }
}
